﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Animation_Manager
{
    class RectDragger
    {
        public enum Direction
        {
            Any,
            Horizontal,
            Vertical
        }

        public static void Init(Control myControl)
        {
            Init(myControl, Direction.Any);
        }

        public static void Init(Control myControl, Direction direction)
        {
            Init(myControl, myControl, direction);
        }

        public static void Init(Control myControl, Control container, Direction direction)
        {
            bool isDragging = false;
            Point startPoint = Point.Empty;
            myControl.MouseDown += delegate(object sender, MouseEventArgs e)
            {
                isDragging = true;
                startPoint = new Point(e.X, e.Y);
                myControl.Capture = true;
            };
            myControl.MouseUp += delegate(object sender, MouseEventArgs e)
            {
                isDragging = false;
                myControl.Capture = false;
            };
            myControl.MouseMove += delegate(object sender, MouseEventArgs e)
            {
                if (isDragging)
                {
                    if (direction != Direction.Vertical)
                        container.Left = Math.Max(0, e.X + container.Left - startPoint.X);
                    if (direction != Direction.Horizontal)
                        container.Top = Math.Max(0, e.Y + container.Top - startPoint.Y);
                }
            };
        }

    }
}
